﻿using Common.Core.Enums;
using Common.Helpers;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using SqlSugar.IOC;

namespace Common.Core.Data
{
    public static class SqlSugarServiceCollectionExtensions
    {
        /// <summary>
        /// 自动注册SqlSugar数据库连接
        /// </summary>
        /// <param name="services"></param>
        public static void AddSqlSugar(this IServiceCollection services)
        {
            var connection = DBServerProvider.GetDBConnection(services);
            var dbConnectionOptions = connection.MassterDB;

            var dbType = IocDbType.MySql;
            switch (connection.MassterDB.DatabaseType)
            {
                case DatabaseType.MySql:
                    dbType = IocDbType.MySql;
                    break;
                case DatabaseType.SqlServer:
                    dbType = IocDbType.SqlServer;
                    break;
                case DatabaseType.SQLite:
                    dbType = IocDbType.Sqlite;
                    break;
                case DatabaseType.PostgreSQL:
                    dbType = IocDbType.PostgreSQL;
                    break;
                case DatabaseType.Oracle:
                    dbType = IocDbType.Oracle;
                    break;
            }

            List<IocConfig> readDBs = null;
            if (connection.IsDBReadWriteSeparate)
            {
                readDBs = new List<IocConfig>();

                foreach (var item in connection.ReadDB)
                {
                    readDBs.Add(new IocConfig()
                    {
                        ConnectionString = item.ConnectionString,
                        DbType = dbType,
                        // 自动释放
                        IsAutoCloseConnection = true,
                    });
                }
            }

            services.AddSqlSugar(new IocConfig()
            {
                ConnectionString = dbConnectionOptions.ConnectionString,
                DbType = dbType,
                // 自动释放
                IsAutoCloseConnection = true,
                // 从库
                SlaveConnectionConfigs = readDBs
            });

            services.ConfigurationSugar(db =>
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    if (connection.PrintSqlLog)
                    {
                        //LogHelper.Debug(sql + "\r\n" + pars.ToDictionary(it => it.ParameterName, it => it.Value).ToJson());
                        //获取原生SQL推荐 5.1.4.63  性能OK
                        LogHelper.Debug(UtilMethods.GetNativeSql(sql, pars));

                        if (connection.PrintDebugSqlLog)
                        {
                            //获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
                            LogHelper.Debug(UtilMethods.GetSqlString(DbType.SqlServer, sql, pars));
                        }
                    }
                };
                //设置更多连接参数
                //db.CurrentConnectionConfig
            });
        }
    }
}
